//
//  MainView.m
//  RealatorView
//
//  Created by Ricky Jones on 1/24/09.
//  Copyright 2009 Vaporsoftware. All rights reserved.
//

#import "LocationsViewController.h"
#import "DataAccess.h"
#import "Location.h"
#import "LocationViewController.h"
#import "RealatorViewAppDelegate.h"
@implementation LocationsViewController


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)dealloc {
    [super dealloc];
}




@synthesize GetLocationsButton = _getLocationsButton;
@synthesize LocationsTable = _locationsTable;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [_locations count] + 1;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	RealatorViewAppDelegate *appDelegate = (RealatorViewAppDelegate *)[[UIApplication sharedApplication] delegate];
	if([indexPath indexAtPosition:1] == 0)
	{
		[appDelegate ShowLocation:nil];		
	}
	else
	{
		[appDelegate ShowLocation:[_locations objectAtIndex:indexPath.row - 1]];
	}
}
#pragma mark Table Delete and Data Source  

// The accessory type is the image displayed on the far right of each table cell. In order for the delegate method
// tableView:accessoryButtonClickedForRowWithIndexPath: to be called, you must return the "Detail Disclosure Button" type.
- (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
{
	return UITableViewCellAccessoryDisclosureIndicator;

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.row == 0)
		return UITableViewCellEditingStyleNone;
	else
		return UITableViewCellEditingStyleDelete;
}

	- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
	{	
		
		UITableViewCell *cell = [self.LocationsTable  dequeueReusableCellWithIdentifier:@"MyIdentifier"];
		if (cell == nil) {
			// Create a new cell. CGRectZero allows the cell to determine the appropriate size.
			cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
			[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
		}
		
		if(indexPath.row==0)
		{
			[cell setText:@"Add new location"];
		}	
		else if([_locations count] > 0)
		{
			
			int index = [indexPath indexAtPosition:1];
			Location* l = [_locations objectAtIndex:index - 1];
			[cell setText:[l Name]];			
		}
		return cell;
	}

	- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
			forRowAtIndexPath:(NSIndexPath *)indexPath {
		@try
		{
			if (editingStyle == UITableViewCellEditingStyleDelete) 
			{
				// Find the book at the deleted row, and remove from application delegate's array.
				Location *locationToDelete = [_locations objectAtIndex:(indexPath.row - 1)];
				DataAccess * data = [[DataAccess alloc] init];
				[data DeleteLocation:[locationToDelete LocationID]];
				[_locations removeObject:locationToDelete];
				// Animate the deletion from the table.
				[self.LocationsTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
				 withRowAnimation:UITableViewRowAnimationFade];
			}
		}
		@catch (NSException* ex) 
		{
			UIAlertView *charAlert = [[UIAlertView alloc]
									  initWithTitle:@"An Error Occured"
									  message:[ex reason]
									  delegate:nil
									  cancelButtonTitle:@"Done"
									  otherButtonTitles:nil];
			
			[charAlert show];
			[charAlert autorelease];
		}
	}


	- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
	{
		[_getLocationsButton setTitle:@"Apply Image" forState:UIControlStateNormal];
		DataAccess * data = [[DataAccess alloc] init];
		_locations = [data GetLocations:@"anyArea"];
		[_locationsTable reloadData];	
		
	}



@end




